import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");
Page({

  data: {
    region: ['吉林省', '长春市', '南关区'],
    customItem: '全部'
  },

  onLoad: function (options) {
    this.initValidate();
  },

  // 提示信息
  initValidate() {
    let rules = {
      name: {
        required: true
      },
      phone: {
        required: true,
        tel: true,
      },
      company: {
        required: true,
      }
    }
    let message = {
      name: {
        required: "请输入姓名",
      },
      phone: {
        required: "请输入手机号",
        tel: "请输入正确手机号",
      },
      company: {
        required: "请输入公司名称",
      }
    }
    this.WxValidate = new WxValidate(rules, message)
  },

  //省市区
  bindRegionChange: function (e) {

    this.setData({
      region: e.detail.value
    })
  },

  // 失败
  showModal(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },

  //提交订单
  formSubmit: function (e) {
    let that=this

    if (!this.WxValidate.checkForm(e)) {
      let err = this.WxValidate.errorList[0]
      this.showModal(err);
      return false;
    }

    wx.showLoading({
      title: '加载中...'
    })
    api.insertSelective2({
      cityId: wx.getStorageSync("cityId"),
      createid: wx.getStorageSync("openId"),
      name : e.detail.value.name,
      phone: e.detail.value.phone,
      companyName : e.detail.value.company,
      agentCity: that.data.region.join(' '), 
    }).then(res => {
      if (res.result) {
        //动画隐藏
        wx.hideLoading();
        wx.showToast({
          title: '提交成功',
          icon: 'none',
          success: function () {
            setTimeout(function () {
              wx.reLaunch({
                url: '../my/my',
              })
            }, 2000);
          }
        })
      }
    })
  }
})